﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    partial class ManagerClienti : Form
    {
        //managerClienti este de tip singleton, adica doar o singura instantiere a managerului pentru clienti poate exista
        private static Form managerClienti = null;

        private ManagerClienti()
        {
            InitializeComponent();
            //managementClienti = this;
        }

        public static Form getManagerClienti()
        {
            if (managerClienti == null)
                managerClienti = new ManagerClienti();
            return managerClienti;
        }

        private void clear()
        {
            textNume.ResetText();
            textPrenume.ResetText();
            textVarsta.ResetText();
            richTextBox1.ResetText();
        }

        private void buttonAdauga_Click(object sender, EventArgs e)
        {
            try
            {
                Clienti c = Clienti.InstantiereClienti();
                if (c.addClient(this.textNume.Text,textPrenume.Text,Convert.ToInt32(textVarsta.Text))== true)
                    MessageBox.Show("Client inregistrat!", "Succes!", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                else
                    MessageBox.Show("Client Existent!", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1);
            }
            catch
            {
                MessageBox.Show("Date invalide!", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
            }

            clear();
        }

        private void buttonListare_Click(object sender, EventArgs e)
        {
            clear();

            Clienti c = Clienti.InstantiereClienti();
            richTextBox1.Text = c.print();
        }

        private void buttonSterge_Click(object sender, EventArgs e)
        {
            try
            {
                Clienti c = Clienti.InstantiereClienti();

                c.rmClient(textNume.Text, textPrenume.Text, Convert.ToInt32(textVarsta.Text));
                    
                MessageBox.Show("Client sters!", "Succes!", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
            }
            catch
            {
                MessageBox.Show("Date invalide!", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
            }

            clear();
        }

        private void buttonEditare_Click(object sender, EventArgs e)
        {
            try
            {
                Clienti clienti = Clienti.InstantiereClienti();
                Client c = clienti.getClient(textNume.Text, textPrenume.Text, Convert.ToInt32(textVarsta.Text));

                if (c == null)
                     MessageBox.Show("Client inexistent", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
                else
                {
                 //   managementClienti.Hide();
                    Form managementClient = new ManagerClient(c);
                    this.Hide();
                    managementClient.Show();
                }
            }
            catch
            {
                MessageBox.Show("Date invalide!", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
            }

            clear();
        }

        private void Exit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
